package com.example.gulimall.gateway.config;

import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.GatewayCallbackManager;
import com.alibaba.fastjson.JSONObject;
import com.example.common.utils.R;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

/**
 * <p>
 *      使用 webflux 响应式编程
 * </p>
 *
 * @author zyred
 * @since v 0.1
 **/
@Configuration
public class GatewaySentinelConfig {

    public GatewaySentinelConfig() {

        GatewayCallbackManager.setBlockHandler(
                // 网关限流，会调用该回调方法
                (exchange, t) -> {
                    R error = R.error();
                    String errorJson = JSONObject.toJSONString(error);
                    // 发布者
                    Mono<String> publisher = Mono.just(errorJson);
                    return ServerResponse.ok().body(publisher, String.class);
                }
        );

    }
}
